Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

106
Views
how to build an object [key, value] use string got form page

I want get two element from page and put into one object

const entryElements = await page.$$('div.kpi-entry');
const contents = await Promise.all(
entryElements.map(async (element) => {
  const objValue= await (await element.$('span.text-xl'))?.innerText();
  const objKey= await (await element.$('eui-base-v0-tooltip'))?.innerText();
  const kpiObject = {objValue,objKey,};
  return obj;
})

);

obj returns

  { objValue: '1', objKey: 'A' },
  { objValue: '2', objKey: 'B' },
  { objValue: '3', objKey: 'C' },
  { objValue: '4', objKey: 'D' },
  { objValue: '5', objKey: 'E' }

but I expected

{
  A: '1',
  B: '2',
  C: '3',
  D: '4',
  E: '5',
}

what is the best wat to do this

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I only adapted your own code, you should check MDN Working with Objects for more details.

const entryElements = await page.$$('div.kpi-entry');
const newObject = {};
const contents = await Promise.all(
entryElements.forEach(async (element) => {
  const objValue= await (await element.$('span.text-xl'))?.innerText();
  const objKey= await (await element.$('eui-base-v0-tooltip'))?.innerText();
  newObject[objKey] = objValue;
})

console.log(newObject)
about 4 years ago · Juan Pablo Isaza Report

0

let obj = [
  { objValue: "1", objKey: "A" },
  { objValue: "2", objKey: "B" },
  { objValue: "3", objKey: "C" },
  { objValue: "4", objKey: "E" },
  { objValue: "5", objKey: "F" },
];

let map = obj.map(element => {
  return { [element.objKey]: element.objValue };
});
console.log(Object.assign(...map));

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!